package com.hospital.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hospital.util.DbUtil;
@WebServlet("/manage/InhospitableServlet")
public class InhospitableServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String method = request.getParameter("method");
		if ("add".equals(method)) {
			String houseId1 = request.getParameter("houseId1").trim();
			String houseId2 = request.getParameter("houseId2").trim();
			String houseId3 = request.getParameter("houseId3").trim();
			String num = request.getParameter("num").trim();
			String houseId  = houseId1 + houseId2 + houseId3 + num ;
			String count = request.getParameter("count");
			String version = request.getParameter("version");
			String address = houseId1+"号楼"+houseId2+"层数"+houseId3+"区域";
			String money = request.getParameter("money");
			String sql = "insert into t_bedhouse(houseId , count, amount ,version, address, money) values('"
				+ houseId +"', '" + count + "', '" + count+ "', '" + version + "','" + address + "','" + money +"')";
			DbUtil.add_modify(sql);
			request.getRequestDispatcher("/manage/bedhouse_info_manage.jsp").forward(request, response);
		} else if ("del".equals(method)) {
			String[] id = request.getParameterValues("selectFlag");
			DbUtil.del("t_bedhouse", id);
			request.getRequestDispatcher("/manage/bedhouse_info_manage.jsp").forward(request, response);
		} else if ("modify".equals(method)) {
			int id = Integer.parseInt(request.getParameter("id"));
			String count = request.getParameter("count");
			String version = request.getParameter("version");
			String sql = "update t_bedhouse set amount = "+count+ "-count+amount, count = '" + count + "', version = '" + version + "' where id = " + id;
			DbUtil.add_modify(sql);
			request.getRequestDispatcher("/manage/bedhouse_info_manage.jsp").forward(request, response);
		} else if("addsickbed".equals(method)) {
			String bedhouseId = request.getParameter("bedhouseId").trim();
			String sickbedId1 = request.getParameter("sickbedId1").trim();
			String sickbedId  = bedhouseId + sickbedId1;
			String state = request.getParameter("state");
			String deptname = request.getParameter("deptname");
			String salemoney = request.getParameter("salemoney");
			String amount = null;
			String sqlhouse = "select * from t_bedhouse where houseId = "+ bedhouseId;
			List<Map<String, Object>> listhouse = DbUtil.find(sqlhouse);
			Map<String, Object> maphouse = null;
			for (int i = 0; i < listhouse.size(); i++) {
				maphouse = (Map<String, Object>)listhouse.get(i);
				amount = (String)maphouse.get("amount");
			}
			int i = Integer.parseInt(amount);
			if(i == 0){
				request.setAttribute("message", "该病房已经无剩余床位,请重新选择病房分配");
				request.getRequestDispatcher("/manage/sickbed_info_add.jsp").forward(request, response);
			}else{
				String sql = "insert into t_sickedbed(bedhouseId , sickbedId, state, deptname, salemoney) values('"			
					+ bedhouseId +"', '" + sickbedId + "', '" + state + "','" + deptname + "','" + salemoney +"')";
				DbUtil.add_modify(sql);
				String sqlamount = "update t_bedhouse set amount =  '" + (i-1) + "' where houseId = " + bedhouseId;
				DbUtil.add_modify(sqlamount);
				request.getRequestDispatcher("/manage/sickbed_info_manage.jsp").forward(request, response);
			}
		} else if ("delsickbed".equals(method)) {
			String[] id = request.getParameterValues("selectFlag");
			String houseId = null;
			String sqlhouseId = "select * from t_sickedbed where id = "+ id[0];
			List<Map<String, Object>> listhouseId= DbUtil.find(sqlhouseId);
			Map<String, Object> maphouseId = null;
			for (int i = 0; i < listhouseId.size(); i++) {
				maphouseId = (Map<String, Object>)listhouseId.get(i);
				houseId = (String)maphouseId.get("bedhouseId");
			}
			DbUtil.del("t_sickedbed", id);
			String amount = null;
			String sqlhouse = "select * from t_bedhouse where houseId = "+ houseId;
			List<Map<String, Object>> listhouse = DbUtil.find(sqlhouse);
			Map<String, Object> maphouse = null;
			for (int i = 0; i < listhouse.size(); i++) {
				maphouse = (Map<String, Object>)listhouse.get(i);
				amount = (String)maphouse.get("amount");
			}
			int i = Integer.parseInt(amount);
			String sqlamount = "update t_bedhouse set amount =  '" + (i+id.length) + "' where houseId = " + houseId;;
			DbUtil.add_modify(sqlamount);
			request.getRequestDispatcher("/manage/sickbed_info_manage.jsp").forward(request, response);
		} else if ("modifysickbed".equals(method)) {
			int id = Integer.parseInt(request.getParameter("id"));
			String deptname = request.getParameter("deptname");
			String salemoney = request.getParameter("salemoney");
			String sql = "update t_sickedbed set deptname = '" + deptname + "', salemoney = '" + salemoney + "' where id = " + id;
			DbUtil.add_modify(sql);
			request.getRequestDispatcher("/manage/sickbed_info_manage.jsp").forward(request, response);
		}
	}
}